package pack1_singleton;
import java.util.ArrayList;
import java.util.Random;

class MultiEmperors {
    //定义最多能产生的实例数量
    private static int maxNumOfEmperor = 2;
    //每个皇帝都有名字，使用一个ArrayList来容纳，每个对象的私有属性
    private static ArrayList<String> nameList=new ArrayList<String>();
    //定义一个列表，容纳所有的皇帝实例
    private static ArrayList<MultiEmperors> emperorList=new ArrayList<MultiEmperors>();
    //当前皇帝序列号
    private static int countNumOfEmperor = 0;

    //产生所有的对象
    static{
        for(int i = 0; i < maxNumOfEmperor; i++){
        	emperorList.add(new MultiEmperors("皇"+(i+1)+"帝"));
    	}
    }
    private MultiEmperors(){
    	//世俗和道德约束你，目的就是不产生第二个皇帝
    }
  
    //传入皇帝名称，建立一个皇帝对象
    private MultiEmperors(String name){
    	nameList.add(name);
    }

    //随机获得一个皇帝对象
    public static MultiEmperors getInstance(){
    	Random random = new Random();
    	//随机拉出一个皇帝，只要是个精神领袖就成
    	countNumOfEmperor = random.nextInt(maxNumOfEmperor);
        //国王的演讲
        say();
    	return emperorList.get(countNumOfEmperor);
    }
    //皇帝发话了
    public static void say(){
    	System.out.println("我是" + nameList.get(countNumOfEmperor));
    }
}

class Minister3{
    private MultiEmperors emperors = null; 
    public MultiEmperors getEmperors(){
        return MultiEmperors.getInstance();
    }
}

public class code003_multiSingleton {
    public static void main(String[] args) {
        int minister_count = 5;
        for (int i = 0; i < minister_count; i++) {
            Minister3 minister = new Minister3();
            minister.getEmperors();
        }
    }
}

